package NiuKe;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * 给定一个字符串 s ，请你找出其中不含有重复字符的 最长子串 的长度。
 */
public class LengthOfLongestSubstring {
    public static void main(String[] args) {
        System.out.println(solution2("abcabd"));
    }

    public static int solution2(String s) {
        // 记录字符上一次出现的位置
        int[] last = new int[26];
        for(int i = 0; i < 26; i++) {
            last[i] = -1;
        }
        int n = s.length();

        int res = 0;
        int start = 0; // 窗口开始位置
        for(int i = 0; i < n; i++) {
            int index = s.charAt(i) - 97;
            start = Math.max(start, last[index] + 1);
            res   = Math.max(res, i - start + 1);
            last[index] = i;
        }

        return res;
    }
    public static int solution(String s){
        char[] charArray = s.toCharArray();
        int pre = 0;
        int cur = 1;
        int max = 1;
        QueueSet queueSet = new QueueSet();
        queueSet.enqueue(charArray[pre]);
        while (cur < charArray.length){
            Boolean contain = queueSet.contain(charArray[cur]);
            if (contain){
                while (charArray[pre] != charArray[cur]){
                    queueSet.dequeueHead(charArray[pre++]);
                }
                pre++;
            }
            queueSet.enqueue(charArray[cur++]);
            if (max < queueSet.quequeSet.size()){
                max = queueSet.quequeSet.size();
            }
        }
        return max;
    }
    static class QueueSet{
        Set<Character> quequeSet = new HashSet<Character>(100);

        public boolean isNull(){
            return quequeSet.size() <= 0 ? true : false;
        }
        public void enqueue(Character en){
            quequeSet.add(en);
        }
        public void dequeueHead(Character de){
            quequeSet.remove(de);
        }
        public Boolean contain(Character co){
            return quequeSet.contains(co);
        }
    }
}
